package com.frejoys.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.AdminVerifyDto;
import com.frejoys.common.dto.admin.AdminVerifyListDto;
import com.frejoys.common.enums.EAdminVerifyType;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.admin.AdminVerifyActivityListVo;
import com.frejoys.common.vo.admin.AdminVerifyGoodsListVo;
import com.frejoys.common.vo.admin.AdminVerifyStoreEnterListVo;
import com.frejoys.common.vo.admin.AdminVerifyStoreListVo;
import com.frejoys.service.AdminVerifyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "审核", description = "店铺入驻,店铺,商品,活动的审核管理")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixAdmin)
@Authorization(Authority.Private)
public class AdminVerifyController {

    private final AdminVerifyService adminVerifyService;

    private final RedissonClient redissonClient;

    @PostMapping("/adminVerify/storeEnter/list")
    @Operation(summary = "查询店铺入驻审核列表")
    public Result<IPage<AdminVerifyStoreEnterListVo>> storeEnterList(@RequestBody @Validated AdminVerifyListDto dto){
        dto.setType(EAdminVerifyType.storeEnter.getValue());
        return Result.success(adminVerifyService.listStoreEnterInfo(dto));
    }
    @PostMapping("/adminVerify/store/list")
    @Operation(summary = "查询店铺上架审核列表")
    public Result<IPage<AdminVerifyStoreListVo>> storeList(@RequestBody @Validated AdminVerifyListDto dto){
        dto.setType(EAdminVerifyType.store.getValue());
        return Result.success(adminVerifyService.listStoreInfo(dto));
    }
    @PostMapping("/adminVerify/goods/list")
    @Operation(summary = "查询商品上架审核列表")
    public Result<IPage<AdminVerifyGoodsListVo>> goodsList(@RequestBody @Validated AdminVerifyListDto dto){
        dto.setType(EAdminVerifyType.goods.getValue());
        return Result.success(adminVerifyService.listGoodsInfo(dto));
    }

    @PostMapping("/adminVerify/activity/list")
    @Operation(summary = "查询活动上架审核列表")
    public Result<IPage<AdminVerifyActivityListVo>> activityList(@RequestBody @Validated AdminVerifyListDto dto){
        dto.setType(EAdminVerifyType.activity.getValue());
        return Result.success(adminVerifyService.listActivityInfo(dto));
    }


    @PostMapping("/adminVerify/verify")
    @Operation(summary = "审核操作")
    public Result<String> list(@RequestBody @Validated AdminVerifyDto dto){
        RLock lock = redissonClient.getLock(CacheKeyUtil.adminVerifyLockCacheKey(RequestUtil.userId(), dto.getId()));
        if (lock.tryLock()) {
            try {
                adminVerifyService.adminVerify(dto);
            } finally {
                lock.unlock();
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }

}
